
/* 换床  */
function showLiveMember(className){
	showPop(className)

	var currentdate = getNowFormatDate();
	$("#liveMemberForm input[name=startDate]").val(currentdate);
									
}
// 获取当前时间——格式：2012-12-06
function getNowFormatDate() {
    var date = new Date();
    var seperator = "-";
    var month = date.getMonth() + 1;
    var day = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (day >= 0 && day <= 9) {
    	day = "0" + day;
    }
    var currentdate = date.getFullYear() + seperator + month + seperator + day;
    return currentdate;
}

/* 弹窗 */
//显示弹框
function showPop(className){
	$(".zhy_zhezhao_pop").show();
	var top = document.body.scrollTop||document.documentElement.scrollTop;
	var this_h = $("." + className).height();
	var win_h = ($('html',parent.document).height()-this_h-100)/2
	$(".zhy_zhezhao_pop").height($("html").height());
	var this_h = $("." + className).height();
	top= top +win_h;
	$("." + className).css("top", top).show().siblings(".zhy_popa").hide();
}

// 隐藏修改弹框
function hidePop(className) {    
	$(".zhy_zhezhao_pop").hide();
	$(".zhy_popa").hide();
	$("."+className).empty();
}

/* 计算费用 */
// 计算床位费用
var payBedCost;
var payBedPreCost;
var payOtherCost;
function calculateBedCost(currBedPrice,accountRemain,bedCostRemain,oldBedPrice){
	currBedPrice = parseFloat(currBedPrice);
	accountRemain = parseFloat(accountRemain);
	bedCostRemain = parseFloat(bedCostRemain);
	oldBedPrice = parseFloat(oldBedPrice);
	
	var startDate = $("#liveMemberForm input[name=startDate]").val();
	var endDate = $("#liveMemberForm input[name=endDate]").val();			
       
	if (startDate != "" && endDate != ""){
		var dateDiff = (Date.parse(endDate.replace(/-/g, "/")) - Date.parse(startDate.replace(/-/g, "/")))/86400000;
		var bedCost = currBedPrice * dateDiff;

		if (bedCost >= 0 && dateDiff >= 0){
			$("#liveMemberForm #bedCost").text(bedCost + "元");
			$("#liveMemberForm input[name=bedCost]").val(bedCost);
			
			// 床位费用
			var currentdate = getNowFormatDate();
			var oldDateDiff = (Date.parse(startDate.replace(/-/g, "/")) - Date.parse(currentdate.replace(/-/g, "/")))/86400000;
			var realBedCostRemain = bedCostRemain - oldBedPrice * oldDateDiff;
			var accountAllRemain = accountRemain+realBedCostRemain;
			if (bedCost >= accountAllRemain){
				$("#backBedCostDisplay").css("display", "none")
				$("#backBedCost").text("");
				
				$("#payBedCostDisplay").css("display", "block")
				payBedCost = bedCost - accountAllRemain;
				$("#payBedCost").text(payBedCost);						
			}else{
				$("#payBedCostDisplay").css("display", "none")
				$("#payBedCost").text("");
				
				$("#backBedCostDisplay").css("display", "block")
				var backBedCost = accountAllRemain - bedCost;
				$("#backBedCost").text(backBedCost);
				payBedCost = -backBedCost;
			}		
		}else{
			$("#liveMemberForm #bedCost").text("");
			$("#liveMemberForm input[name=bedCost]").val("");
		}
	}

	if (payBedCost != null && payBedPreCost != null && payOtherCost != null){
		changeTotalCost();
	}else{
		$("#payTotalCostDisplay").css("display", "none")
		$("#payTotalCost").text("");
		$("input[name=payTotalCost").val("");

		$("#backTotalCostDisplay").css("display", "none")
		$("#backTotalCost").text("");
		$("input[name=backTotalCost").val("");
		
		$("#realPayCostDisplay").css("display", "none")	
		$("#oweReasonDisplay").css("display", "none");		
	}
}
// 计算预付押金
function calculatePreCost(oldBedPreCost){
	oldBedPreCost = parseFloat(oldBedPreCost);
	var obj = $("input[name=bedPreCost]");

	var newBedPreCost = $("input[name=bedPreCost]").val();

	if (parseFloat(newBedPreCost) >= oldBedPreCost){
		$("#payBedPreCostDisplay").css("display", "block")
		payBedPreCost = newBedPreCost - oldBedPreCost;
		$("#payBedPreCost").text(payBedPreCost);

		$("#backBedPreCostDisplay").css("display", "none")
		$("#backBedPreCost").text("");		

		checkNum(obj, "bedPreCostError");
		//$("#liveMemberForm #bedPreCostError").css("display", "none");
	}else if (parseFloat(newBedPreCost) < oldBedPreCost){
		$("#backBedPreCostDisplay").css("display", "block")
		var backBedPreCost = oldBedPreCost - newBedPreCost;
		$("#backBedPreCost").text(backBedPreCost);
		payBedPreCost = -backBedPreCost;

		$("#payBedPreCostDisplay").css("display", "none")
		$("#payBedPreCost").text("");

		checkNum(obj, "bedPreCostError");
		//$("#liveMemberForm #bedPreCostError").css("display", "none");
	}else {
		$("#payBedPreCostDisplay").css("display", "none")
		$("#payBedPreCost").text("");

		$("#backBedPreCostDisplay").css("display", "none")
		$("#backBedPreCost").text("");		

		checkNum(obj, "bedPreCostError");
//			$("#liveMemberForm #bedPreCostError").css("display", "block");
	}
	
	if (payBedCost != null && payBedPreCost != null && payOtherCost != null){
		changeTotalCost();
	}else{
		$("#payTotalCostDisplay").css("display", "none")
		$("#payTotalCost").text("");
		$("input[name=payTotalCost").val("");

		$("#backTotalCostDisplay").css("display", "none")
		$("#backTotalCost").text("");
		$("input[name=backTotalCost").val("");
		
		$("#realPayCostDisplay").css("display", "none")	
		$("#oweReasonDisplay").css("display", "none");		
	}
}
// 计算其它费用
function calculateOtherCost(oldOtherCost){
	oldOtherCost = parseFloat(oldOtherCost);
	var obj = $("input[name=otherCost]");
	var newOtherCost = $("input[name=otherCost]").val();

	if (parseFloat(newOtherCost) >= oldOtherCost){
		$("#payOtherCostDisplay").css("display", "block")
		payOtherCost = newOtherCost - oldOtherCost;
		$("#payOtherCost").text(payOtherCost);

		$("#backOtherCostDisplay").css("display", "none")
		$("#backOtherCost").text("");

		checkNum(obj, "otherCostError");
//			$("#liveMemberForm #otherCostError").css("display", "none");
	}else if (parseFloat(newOtherCost) < oldOtherCost){
		$("#backOtherCostDisplay").css("display", "block")
		var backOtherCost = oldOtherCost - newOtherCost;
		$("#backOtherCost").text(backOtherCost);
		payOtherCost = -backOtherCost;

		$("#payOtherCostDisplay").css("display", "none")
		$("#payOtherCost").text("");

		checkNum(obj, "otherCostError");
//			$("#liveMemberForm #otherCostError").css("display", "none");
	}else {
		$("#payOtherCostDisplay").css("display", "none")
		$("#payOtherCost").text("");

		$("#backOtherCostDisplay").css("display", "none")
		$("#backOtherCost").text("");

		checkNum(obj, "otherCostError");
//			$("#liveMemberForm #otherCostError").css("display", "block");
	}
	
	if (payBedCost != null && payBedPreCost != null && payOtherCost != null){
		changeTotalCost();
	}else{
		$("#payTotalCostDisplay").css("display", "none")
		$("#payTotalCost").text("");
		$("input[name=payTotalCost").val("");

		$("#backTotalCostDisplay").css("display", "none")
		$("#backTotalCost").text("");
		$("input[name=backTotalCost").val("");
		
		$("#realPayCostDisplay").css("display", "none")	
		$("#oweReasonDisplay").css("display", "none");		
	}
}
// 总费用变更处理
function changeTotalCost(){
	var totalCost = payBedCost + payBedPreCost + payOtherCost;
	
	if (totalCost > 0){
		$("#payTotalCostDisplay").css("display", "block")
		$("#payTotalCost").text(totalCost);		
		$("input[name=payTotalCost").val(totalCost);		

		$("#backTotalCostDisplay").css("display", "none")
		$("#backTotalCost").text("");
		$("input[name=backTotalCost").val("");
		
		$("#realPayCostDisplay").css("display", "block")	
	}else{
		$("#backTotalCostDisplay").css("display", "block")
		$("#backTotalCost").text(-totalCost);
		$("input[name=backTotalCost").val(-totalCost);

		$("#payTotalCostDisplay").css("display", "none")
		$("#payTotalCost").text("");
		$("input[name=payTotalCost").val("");

		$("#realPayCostDisplay").css("display", "none")	
		$("#oweReasonDisplay").css("display", "none");		
	}
	
}
// 实缴费用-是否显示欠费原因
function realPayTotalCost(){
	var payCost = $("#payTotalCost").text();
	var realPayCost = $("input[name=realPayCost]").val();

	var realPayCostObj = $("input[name=realPayCost]");
	if (parseFloat(realPayCost) < parseFloat(payCost)){
		$("#oweReasonDisplay").css("display", "block");
		checkNum(realPayCostObj, "realPayCostError");
	}else{
		$("#oweReasonDisplay").css("display", "none");	
		checkNum(realPayCostObj, "realPayCostError");
	}
}

// 检查是否为空
function checkIsEmpty(obj, errorId) {
	var value = obj.val();
	if (null == value || value == ""){
		$("#liveMemberForm #"+errorId).css("display", "block");
		return false;
	}else{
		$("#liveMemberForm #"+errorId).css("display", "none");
		return true;
	}
}

// 检查是否为：非负整数或最多精确到两位的小数
function checkNum(obj, errorId) {
	var value = obj.val();
    if($.trim(value).length == 0){
		$("#liveMemberForm #"+errorId).css("display", "block");
		return false;
    }
    
    if((value.indexOf(".")+1) != value.length){
	    var regex=/^[0-9]+([.]{1}[0-9]{1,2})?$/;
		if(regex.test(value)){
			$("#liveMemberForm #"+errorId).css("display", "none");
			return true;
		}else{
			$("#liveMemberForm #"+errorId).css("display", "block");			
			obj.val("");
			return false;
	   }  
    }
}

//校验换床表单
function liveMemberFormSubmit(){
	 var startDate = $("input[name='startDate']").val();
	 var endDate = $("input[name='endDate']").val();
	 if (startDate > endDate) {
		 zhyAlert("开始日期不能大于结束日期");
	     return false;
	 }

	var bedPreCostObj = $("input[name=bedPreCost]");
	var bedPreCostFlag = checkNum(bedPreCostObj, "bedPreCostError");
	if (!bedPreCostFlag){
		return false;
	}

	var otherCostObj = $("input[name=otherCost]");
	var otherCostFlag = checkNum(otherCostObj, "otherCostError");
	if (!otherCostFlag){
		return false;
	}
	
	var realPayCostFlag = true;
	var realPayCostDisplay = $("#realPayCostDisplay").css("display")	
	if (realPayCostDisplay == "block") {
		var obj = $("input[name=realPayCost]");
		realPayCostFlag = checkNum(obj, "realPayCostError");
		if (!realPayCostFlag){
			return false;
		}
	}
	
	var oweReasonFlag = true;
	var oweReasonDisplay = $("#oweReasonDisplay").css("display");
	if (oweReasonDisplay == "block") {
		var obj = $("#oweReasonDisplay #oweReason");
		oweReasonFlag = checkIsEmpty(obj, "oweReasonError");
		if (!oweReasonFlag){
			return false;
		}
	}

	var realPayCost = $("input[name=realPayCost]").val();
	var payCostMust = parseFloat(payBedPreCost) + parseFloat(payOtherCost);
	if (parseFloat(realPayCost) < payCostMust){
		zhyAlert("请交齐入住押金和其它费用")
		return false;
	}
	
	$("#liveMemberForm").submit();
}
